package kz.simplex.photobox.action;

import kz.simplex.photobox.model.*;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;

@Name("albumHome")
public class AlbumHome extends EntityHome<Album> {

	@In(create = true)
	AccessHome accessHome;
	@In(create = true)
	CategoryHome categoryHome;
	@In(create = true)
	UserHome userHome;

	public void setAlbumId(Integer id) {
		setId(id);
	}

	public Integer getAlbumId() {
		return (Integer) getId();
	}

	@Override
	protected Album createInstance() {
		Album album = new Album();
		return album;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		Access access = accessHome.getDefinedInstance();
		if (access != null) {
			getInstance().setAccess(access);
		}
		Category category = categoryHome.getDefinedInstance();
		if (category != null) {
			getInstance().setCategory(category);
		}
		User owner = userHome.getDefinedInstance();
		if (owner != null) {
			getInstance().setOwner(owner);
		}
	}

	public boolean isWired() {
		if (getInstance().getAccess() == null)
			return false;
		if (getInstance().getOwner() == null)
			return false;
		return true;
	}

	public Album getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

}
